package com.qcu.util;

import lombok.ToString;

import java.io.Serializable;

public class Response<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    // 私有构造函数，防止外部直接实例化
    private Response() {}

    // 成功时调用
    public static <T> Response<T> success(T data) {
        return new Response<>(ResponseCode.SUCCESS, data);
    }

    // 成功时调用，不带数据
    public static <T> Response<T> success() {
        return new Response<>(ResponseCode.SUCCESS, null);
    }

    // 失败时调用
    public static <T> Response<T> failure(ResponseCode responseCode) {
        return new Response<>(responseCode, null);
    }

    // 失败时调用，带自定义消息
    public static <T> Response<T> failure(ResponseCode responseCode, String message) {
        return new Response<>(responseCode, message, null);
    }

    // 私有构造函数
    private Response(ResponseCode responseCode, T data) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
        this.data = data;
    }

    // 私有构造函数
    private Response(ResponseCode responseCode, String message, T data) {
        this.code = responseCode.getCode();
        this.message = message;
        this.data = data;
    }

    // Getters
    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return "{\"code\":" + code + ",\"message\":\"" + message + "\",\"data\":" + data + "}";
    }

}
